Skip to content

Conversation

@artimath
Copy link

@artimath artimath commented Nov 7, 2025

PR: Expose world helpers via workflow/api (v2)

Summary

  • Surface getWorld plus every World helper (runs/steps/events/hooks/queue/streams/start) directly from workflow/api, so application code can manage runs without reimplementing createWorld.
  • Add Vitest coverage that stubs setWorld to assert each helper delegates correctly and specifically exercises the “list runs then cancel” flow requested for Next.js/server usage.
  • Document the new API via a dedicated “World helpers” reference page, updated foundations guide example, and API index entry; include testing guidance about swapping worlds.
  • Record the change with a Changeset so release automation bumps the workflow package.

Use Cases Addressed

  1. Programmatic run management: Next.js routes (or any server code) can list, inspect, pause/resume, or cancel active runs using listRuns, cancelRun, etc., without importing from workflow/runtime.
  2. Accessing the default world: Users asking “is there a public API to access the active World?” can now call getWorld() from workflow/api, matching CLI behavior across dev/test/prod setups.
  3. Queue/stream control: Advanced tooling can enqueue messages or stream data via the same helpers the runtime uses (queue, createQueueHandler, writeToStream, etc.).
  4. Testability: Teams can stub setWorld in tests while still using the exported helpers, ensuring parity with production behavior.

Implementation Details

  • packages/workflow/src/api.ts now imports the relevant types from @workflow/world, re-exports them, and defines thin wrappers for every World subfunction plus startWorld.
  • Added @workflow/world to packages/workflow/package.json dependencies and synced pnpm-lock.yaml.
  • Created packages/workflow/src/api.test.ts with full delegation coverage and the “list -> cancel” scenario.
  • Authored docs/content/docs/api-reference/workflow-api/world.mdx, updated the API index cards, and inserted a new “Programmatically list and control runs” section in starting-workflows.mdx.
  • Added .changeset/expose-world-helpers.md so the workflow package gets a patch bump.

Verification

  • pnpm --filter workflow test -- src/api.test.ts
  • pnpm --filter workflow typecheck
  • pnpm biome check packages/workflow/src/api.ts packages/workflow/src/api.test.ts

Notes

  • The PR remains in draft status until review; description mirrored here for convenience.

@changeset-bot
Copy link

changeset-bot bot commented Nov 7, 2025

🦋 Changeset detected

Latest commit: 60bb0b4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
workflow Patch
@workflow/ai Patch
@workflow/world-testing Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Contributor

vercel bot commented Nov 7, 2025

@artimath is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

@artimath artimath marked this pull request as draft November 7, 2025 11:32
@artimath artimath force-pushed the feature/world-helpers-pr2 branch 2 times, most recently from f0c5331 to ac7b480 Compare November 7, 2025 11:46
@artimath artimath force-pushed the feature/world-helpers-pr2 branch from ac7b480 to 60bb0b4 Compare November 7, 2025 11:47
@artimath artimath marked this pull request as ready for review November 7, 2025 12:47
Copy link
Member

@VaguelySerious VaguelySerious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing!

A lot of the documentation changes are great. We're not sure yet if we want to re-export all the functions top-level, instead, for now, I think we should document getWorld more and maybe make it available under /api too. Since users might eventually run into the world concept anyway, it's less confusing if there's a single way to interact with the API.

We'd be happy to merge this, if those changes are implemented, making this a mostly documentation-focused PR. If so, please also ensure DCO passes on your final commit

Get workflow run status and metadata without waiting for completion.
</Card>
<Card href="/docs/api-reference/workflow-api/world" title="World helpers">
Access the World singleton plus list/cancel APIs for runs, steps, events, and hooks.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Access the World singleton plus list/cancel APIs for runs, steps, events, and hooks.
Access the World API for programmatic access to run, step, event, and hook entities.


This works the same way in development and production—just make sure the usual `WORKFLOW_*` environment variables are present so the correct world implementation can be chosen.

## Helper catalog
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to generate these from the interface, so they don't get outdated. For now, we could just link to the latest interface.ts file in the npm package?


### Programmatically list and control runs

If you want to show workflow progress directly in your product UI—or let trusted operators retry or cancel work—you can now call the same administrative helpers the CLI uses, straight from `workflow/api`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you want to show workflow progress directly in your product UIor let trusted operators retry or cancel work—you can now call the same administrative helpers the CLI uses, straight from `workflow/api`.
If you want to show workflow progress directly in your product UI, or let trusted operators retry or cancel runs, you can call `getWorld()` to access the full low-level API. See the [World helpers reference](/docs/api-reference/workflow-api/world) for a list of capabilities.

}
```

### Programmatically list and control runs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Programmatically list and control runs
### Programmatically query and control runs

If you want to show workflow progress directly in your product UI—or let trusted operators retry or cancel work—you can now call the same administrative helpers the CLI uses, straight from `workflow/api`.

```typescript lineNumbers
import { listRuns, cancelRun } from 'workflow/api';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example should be moved to /docs/api-reference/workflow-api/world and use getWorld instead

/**
* Creates a workflow run using the configured World implementation.
*/
export function createRun(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should encourage users to use getWorld() directly instead, which makes the API more discoverable (i.e. what actions are available on a run? Just type world.runs. to find the list) and allows us to skip the duplication in this file

type Event,
getHookByToken,
getRun,
getWorld,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It think it'd be fine to re-export getWorld and createWorld from src/api for discoverability (in addition to the export already on runtime)


# Accessing the World singleton

> “Is there any public API to access the active default World? I'd like to be able to list active runs in my app, like I can via the CLI. I could store them myself, but if there is an API for this that works across testing and deployment, that'd be even better!”
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's skip this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants